<template>
  <div class="container">
    <div class="search">
      <van-search
        v-model="searchValue"
        @search="onSearch"
        show-action
        :clearable="false"
        placeholder="请输入要查询的通告名称"
      >
        <template #action>
          <div @click="onSearch">搜索</div>
        </template>
      </van-search>
    </div>
    <div class="no-data" v-if="isShowNoData">暂无数据</div>
    <div class="notice">
      <div class="notice-info" v-for="(item,index) in noticeData" :key="index">
        <div @click="handleClick(item.contents)">
          <div class="top">{{ item.title }}</div>
          <div class="bottom">
            <div class="source">来源:{{ item.source }}</div>
            <div class="release-time">发布时间:{{ item.time }}</div>
          </div>
        </div>
      </div>
    </div>
    <!-- <van-pagination v-model="page" :page-count="Math.ceil(total/4)" @change="changePage" mode="simple" v-if="!isShowNoData" /> -->
  </div>
</template>

<script setup>
  import { ref, onMounted } from "vue";
  import { useRouter } from "vue-router";
  const router = useRouter();

  // 引入api
  import {
    allWaterWayNotice, //所有航道通告
  } from "@/bridge-h5/utils/myapi";

  const noticeData = ref([]);
  const searchValue = ref('');
  const isShowNoData = ref(false);
  const page = ref(1);
  const size = ref(10000);
  const total = ref(0);
  const onSearch = () => {
    page.value = 1;
    allWaterWayNoticeFunction();
  };
  const changePage = (value) => {
    page.value = value;
    allWaterWayNoticeFunction();
  };
  const allWaterWayNoticeFunction = () => {
    allWaterWayNotice(searchValue.value, '', '', '', page.value, size.value).then((res) => {
      if(res.code === 200 && res.data){
        total.value = res.data.totalPage;
        if(res.data.data.length){   
          noticeData.value = res.data.data;
          isShowNoData.value = false; 
        }else{          
          noticeData.value = [];
          isShowNoData.value = true;
        }              
      }
    });
  }; 
  const handleClick = (contents) => {
    let reg =  /<a[^>]+href\s*=\s*['"]([^"']+)['"][^>]*>/gi;   
    if(contents.match(reg)){        
      // window.open(`http://210.76.81.180/traffic-channel/notice${contents.match(reg)[0].slice(25,-2)}`);            
      window.open(`${import.meta.env.VITE_APP_BASE_API}notice${contents.match(reg)[0].slice(25,-2)}`);        
    }else{
      let matchReg = /<img.*?(?:>|\/>)/gi;
      let srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
      let data = [];
      let arr = contents.match(matchReg);
      arr.forEach(item => {
        let src = item.match(srcReg);
        // data.push(`http://210.76.81.180/traffic-channel/notice${src[1]}`);
        data.push(`${import.meta.env.VITE_APP_BASE_API}notice${src[1]}`);
      });

      router.push({
        path: "/pdf",
        query: { content: data.join(',') },
      });
    }    
  };

  onMounted(() => {
    allWaterWayNoticeFunction();
  });
</script>

<style lang="scss" scoped>
.container{
  width: 100%;
  overflow-x: hidden;
  // overflow-y: auto;
  // padding-bottom: 50px;
  .header{
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #fff;
    box-shadow: 0px 2px 8px 0px rgba(0,0,0,0.08);
    .title {
      color: #262626;
      font-size: 18px;
      font-weight: 500;
      padding: 12px 0px 10px 20px;
      position: relative;
    }
    .time {
      display: flex;
      align-items: center;
      color: #8C8C8C;        
      font-size: 14px;
      padding-right: 20px;
      img{
        width: 18px;
        height: 18px;
        padding-right: 5px;
      }
    }
  }
  .notice{
    margin-bottom: 100px;
    .notice-info{
      margin: 20px;
      background: #FFFFFF;
      box-shadow: 0px 2px 8px 1px rgba(0,0,0,0.08);
      border-radius: 8px;
      opacity: 1;
      padding: 16px;
      .top{
        color: #000000;
        font-size: 12px;
        font-weight: 400;
      }
      .bottom{
        display: flex;
        justify-content: space-between;
        font-size: 12px;
        zoom: 0.8;
        font-weight: 400;
        color: #8C8C8C;
        margin-top: 10px;
      }
    }
  }
  .search{
    margin: 10px 0px;
    // background: #FFFFFF;
    box-shadow: 0px 2px 8px 1px rgba(0,0,0,0.08);
    border-radius: 8px;
    opacity: 1;
  }
  .no-data{
    display: flex;
    justify-content: center;
    margin: 20px;
    font-size: 14px;
  }
}
</style>